package com.liy.framework.web.rs.filter;

import com.liy.framework.common.utils.ContextHandler;
import java.io.IOException;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.container.ContainerResponseFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ResponseFilter implements ContainerResponseFilter {
    protected static Logger log = LoggerFactory.getLogger(ResponseFilter.class);

    public ResponseFilter() {}

    public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException {
        ContextHandler.remove();
        long startTime = Long.parseLong(requestContext.getProperty("start_time").toString());
        log.info("jaxrs reqponse filter, handle time (ms): " + (System.currentTimeMillis() - startTime));
        if (requestContext.getHeaderString("ping") != null) {
            responseContext.getHeaders().add("pong", "pong");
        }

    }
}
